//
//  CAICameraBottomPreviewPhotoCell.m
//  CAIImagePickerControllerKit
//
//  Created by 海参 on 2022/8/5.
//

#import "CAICameraBottomPreviewPhotoCell.h"
#import "CAIImagePickerKitTool.h"
#import <Masonry/Masonry.h>
#import "CAICameraOverlayBottomView.h"
#import <SDWebImage/SDWebImage.h>

@interface CAICameraBottomPreviewPhotoCell ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UIView *pointView;


@end


@implementation CAICameraBottomPreviewPhotoCell


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.picItemLayoutRight = 9;
        self.picItemLayoutTop = 9;
        [self setUI];
        [self addLayout];
    }
    return self;
}


- (void)setUI{
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.deleteButton];
    [self.contentView addSubview:self.pointView];
}

- (void)addLayout{
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-self.picItemLayoutRight);
        make.top.mas_equalTo(self.contentView.mas_top).offset(self.picItemLayoutTop);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.picItemLayoutRight*2);
        make.height.mas_equalTo(self.picItemLayoutTop *2);
    }];
    
    [self.pointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_bottom).offset([CAICameraBottomPreviewPhotoCell pointViewTop]);
        make.centerX.mas_equalTo(self.imageView.mas_centerX);
        make.size.mas_equalTo([CAICameraBottomPreviewPhotoCell pointViewSize]);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
    self.imageView.layer.cornerRadius = LCDScale_iPhone6(4);
    self.pointView.layer.cornerRadius = [CAICameraBottomPreviewPhotoCell pointViewSize].height/2;
}


+ (CGFloat)pointViewTop{
    return LCDScale_iPhone6(8);
}
+ (CGSize)pointViewSize{
    return CGSizeMake(LCDScale_iPhone6(6), LCDScale_iPhone6(6));
}




- (void)reloadLayout{
   
    [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-self.picItemLayoutRight);
        make.top.mas_equalTo(self.contentView.mas_top).offset(self.picItemLayoutTop);
    }];
    
    [self.deleteButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.picItemLayoutRight*2);
        make.height.mas_equalTo(self.picItemLayoutTop *2);
    }];
}



- (void)setData:(id)data{
    
    if ([data isKindOfClass:[CAIUIImagePickerCameraPhotoModel class]]) {
        CAIUIImagePickerCameraPhotoModel *model = (CAIUIImagePickerCameraPhotoModel *)data;
        data = model.photo;
        if ([data isKindOfClass:[UIImage class]])
        {
            self.imageView.image = (UIImage *)data;
        }
        else if ([data isKindOfClass:[NSString class]]){
            NSURL *url = [NSURL URLWithString:(NSString *)data];
            [self.imageView sd_setImageWithURL:url placeholderImage:nil];
        }
        else{
            self.imageView.image = nil;
        }
        if (model.selected) {
            [self setCellSelected:YES];
        }else{
            [self setCellSelected:NO];
        }
    }
}


- (void)setCellSelected:(BOOL)selected{
    if (selected) {
        self.imageView.layer.borderWidth = 2;
        self.imageView.layer.borderColor =self.selectBorderColor?self.selectBorderColor.CGColor:[UIColor colorWithRed:48/255.0 green:125/255.0 blue:244/255.0 alpha:1.0].CGColor;
        self.pointView.alpha = 1;
    }else{
        self.imageView.layer.borderWidth = 0;
        self.pointView.alpha = 0;
    }
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected) {
        self.imageView.layer.borderWidth = 2;
        self.imageView.layer.borderColor =self.selectBorderColor?self.selectBorderColor.CGColor:[UIColor colorWithRed:48/255.0 green:125/255.0 blue:244/255.0 alpha:1.0].CGColor;
        self.pointView.alpha = 1;
    }else{
        self.imageView.layer.borderWidth = 0;
        self.pointView.alpha = 0;
    }
}

#pragma mark -Setter

- (void)setPointColor:(UIColor *)pointColor{
    if (_pointColor != pointColor ) {
        _pointColor = pointColor;
        self.pointView.layer.backgroundColor = pointColor.CGColor;
    }
}




#pragma mark -Getter


- (UIImageView *)imageView{
    if (!_imageView) {
        UIImageView *view = [[UIImageView alloc] init];
        view.contentMode = UIViewContentModeScaleAspectFill;
        view.clipsToBounds = YES;
        _imageView = view;
    }
    return _imageView;
}

- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[CAIImagePickerKitTool imageNamed:@"delete_icon"] forState:UIControlStateNormal];
        _deleteButton = btn;
    }
    return _deleteButton;
}

- (UIView *)pointView{
    if (!_pointView) {
        UIView *view = [[UIView alloc] init];
        view.layer.backgroundColor = [UIColor colorWithRed:48/255.0 green:125/255.0 blue:244/255.0 alpha:1.0].CGColor;
        _pointView = view;
    }
    return _pointView;
}

@end
